{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### RPY2\n",
    "\n",
    "*rpy2* is a convenient way to execute R-code inside a Python environment. However, this also requires that *R* has been installed on the current system.\n",
    "\n",
    "Like Python, R is free, and can be obtained from\n",
    "https://cran.r-project.org/\n",
    "\n",
    "Note that this is a very rudimentary demonstration, and only demonstrates that R code and graphics can be used quite simply from within Python.\n",
    "\n",
    "author: Thomas Haslwanter\n",
    "date:   April-2020"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rpy2.robjects import r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Programs\\WPy64-3760\\python-3.7.6.amd64\\lib\\site-packages\\rpy2\\robjects\\pandas2ri.py:17: FutureWarning: pandas.core.index is deprecated and will be removed in a future version.  The public classes are available in the top-level namespace.\n",
      "  from pandas.core.index import Index as PandasIndex\n"
     ]
    }
   ],
   "source": [
    "%load_ext rpy2.ipython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%%R\n",
    "X = c(1,2,3,4,5)\n",
    "Y = c(1,2,3,6,5)\n",
    "s = capture.output(summary(lm(Y~X)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "spy = r['s']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lm(formula = Y ~ X)\n",
      "\n",
      "Residuals:\n",
      "        1         2         3         4         5 \n",
      "-2.22e-16 -2.00e-01 -4.00e-01  1.40e+00 -8.00e-01 \n",
      "\n",
      "Coefficients:\n",
      "            Estimate Std. Error t value Pr(>|t|)  \n",
      "(Intercept)  -0.2000     1.0132  -0.197   0.8561  \n",
      "X             1.2000     0.3055   3.928   0.0294 *\n",
      "---\n",
      "Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
      "\n",
      "Residual standard error: 0.9661 on 3 degrees of freedom\n",
      "Multiple R-squared:  0.8372,\tAdjusted R-squared:  0.7829 \n",
      "F-statistic: 15.43 on 1 and 3 DF,  p-value: 0.02937\n"
     ]
    }
   ],
   "source": [
    "for line in spy[2:-1]:\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAACFlBMVEUAAAAAADoAAGYAOjoAOpAAZrYICAgLCwsNDQ0ODg4PDw8RERESEhIUFBQeHh4lJSUmJiYnJycvLy84ODg6AAA6ADo6AGY6Ojo6OmY6OpA6ZrY6kNs7Ozs8PDw9PT0+Pj5AQEBBQUFCQkJDQ0NERERGRkZHR0dISEhJSUlKSkpLS0tMTExOTk5PT09QUFBRUVFSUlJUVFRVVVVWVlZXV1dYWFhaWlpbW1tcXFxdXV1eXl5hYWFjY2NkZGRmAABmADpmAGZmOgBmOpBmZmZmtv9paWlqampra2ttbW1wcHBxcXFzc3N2dnZ4eHh5eXl6enp9fX1+fn5/f3+BgYGHh4eIiIiJiYmNjY2Ojo6QOgCQOjqQOmaQtpCQ29uQ2/+RkZGTk5OUlJSVlZWXl5eYmJiZmZmcnJyenp6fn5+goKChoaGioqKkpKSmpqanp6epqamqqqqrq6usrKytra2urq6wsLCxsbGysrKzs7O1tbW2ZgC2tra2//+4uLi7u7u+vr7BwcHCwsLExMTHx8fJycnNzc3Pz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yYIzMJyB4QwMZ2A4A8MZGM7AcAaGMzCcgeEMDGdgOAPDGRjOwHAGhjMwnIHhDAw3yIFnN89FUT3gD133g0EO3Pnx4ubIXNmzCGugAzdH/pbwVa7qGejA0exe+AV60AMHfi2mHwx04Nb+2+m34MEO3BjCP0QPdOCFPTMhX1vvDwMcePG98vibYbABDjwYDAxnYDgDwxkYzsBwBoYzMJyB4QwMZ2A4A8MZGM7AcAaGMzCcgeEMDGdguP8BdNGhWSKUomkAAAAASUVORK5CYII=\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%R plot(X,Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
